<?php
/**
 * Created by PhpStorm.
 * User: baidu
 * Date: 18/2/28
 * Time: 上午1:39
 */
$http = new swoole_http_server("0.0.0.0", 9501);

$http->set(
    [
        'enable_static_handler' => true,
        'document_root' => "/data/www/tp_swoole/public/static",
        'worker_num' => 5,
    ]
);
$http->on('WorkerStart',function(swoole_server $server,$worker_id){
    //定义目录
    define('APP_PATH', __DIR__ . '/../application/');
    //加载框架文件
    if(file_exists(__DIR__ . '/../thinkphp/base.php')){
        require __DIR__ . '/../thinkphp/base.php';
    }else{
        require __DIR__ . '/../thinkphp/base.php';
    }
    
});
$http->on('request', function($request, $response) use($http){
    if(isset($request->server)){
        foreach ($request->server as $key => $value) {
            $_SERVER[strtoupper($key)] = $value;
        }
    }

    if(isset($request->header)){
        foreach ($request->header as $key => $value) {
            $_SERVER[strtoupper($key)] = $value;
        }
    }

    $_GET = [];
    if(isset($request->get)){
        foreach ($request->server as $key => $value) {
            $_GET[$key] = $value;
        }

    }

    $_POST = [];
    if(isset($request->post)){
        foreach ($request->server as $key => $value) {
            $_POST[$key] = $value;
        }
    }
    

    ob_start();
    try {
        think\Container::get('app',[APP_PATH])
        ->run()
        ->send();
        
    } catch (Exception $e) {
        
    }
    
    $res = ob_get_contents();
    ob_end_clean();
    $response->end($res);
    $http->close();

});

$http->start();